package com.yumeko.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yumeko.springcloud.server.PaymentHytrixService;

import javax.annotation.Resource;

/**
 * @program: SpringCloud
 * @description:
 * @author: Zheng Hao
 * @create: 2020-03-09 21:14
 */
@Slf4j
@RestController
@DefaultProperties(defaultFallback="paymentGloblaFallbackMethead")///降级方式2
public class PaymentHystrixController {
    @Resource
    private PaymentHytrixService paymentHytrixService;

    @RequestMapping("/consumer/payment/hystrix/ok/{id}")
    @HystrixCommand ////降级方式2
    public String paymentInfoOK(@PathVariable("id") Integer id) {
        int i = 10/0;
        String paymentInfoOK = paymentHytrixService.paymentInfoOK(id);
        log.info("========paymentInfoOK: paymentInfoOK");
        return paymentInfoOK;
    }

    public String paymentGloblaFallbackMethead(){
        return "统一设置:宝贝服务有点忙!!8000 paymentInfoTimeOut.id: " + "\t" + "(┬＿┬) ";
    }

    //降级方式1
    @HystrixCommand(fallbackMethod = "paymentInfoTimeOutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
    })
    @RequestMapping("/consumer/payment/hystrix/timeout/{id}")
    public String paymentInfoTimeOut(@PathVariable("id") Integer id) {
        String paymentInfoTimeOut = paymentHytrixService.paymentInfoTimeOut(id);
        log.info("========paymentInfoTimeOut: paymentInfoTimeOut");
        return paymentInfoTimeOut;
    }

    public String paymentInfoTimeOutHandler(Integer id) {
        return "宝贝服务有点忙!!8000 paymentInfoTimeOut.id: " + id + "\t" + "(┬＿┬) ";
    }
}
